package com.niiwoo.civet.trade.service.local.newRefund.notify.mq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.niiwoo.activity.event.dto.request.OverdueProjectEventReqDTO;
import com.niiwoo.activity.event.enums.ProjectStatusEventEnum;
import com.niiwoo.activity.event.enums.ProjectSubStatusEventEnum;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dao.entity.ProjectBorrowerSummary;
import com.niiwoo.civet.trade.dao.mapper.ProjectBorrowerPlanNewMapperExt;
import com.niiwoo.civet.trade.dao.mapper.ProjectBorrowerSummaryMapperExt;
import com.niiwoo.civet.trade.dto.newRefund.RefundStateDTO;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyDTO;
import com.niiwoo.civet.trade.service.local.newRefund.notify.enums.RefundNotifyEnum;
import com.niiwoo.civet.trade.service.local.newRefund.refundDeal.core.FeeCalcForRefundService;

/**
 * 活动中心标的逾期变更MQ通知
 * 
 * 触发条件：标的逾期
 *
 * @author: kevin.jia
 * @version 1.0.0
 */
@Component
public class ActivityEventProjectOverdueNotify extends MQRefundNotify {
	
	@Autowired
	private FeeCalcForRefundService feeCalcForRefundService;
	
	@Autowired
	private ProjectBorrowerPlanNewMapperExt projectBorrowerPlanNewMapper;
	
	@Autowired
	private ProjectBorrowerSummaryMapperExt projectBorrowerSummaryMapper;
	
	@Override
	public Object bulidParam(RefundNotifyDTO refundNotifyDTO) {
		Project project = refundNotifyDTO.getProject();
		
		// 构建MQ信息内容
		OverdueProjectEventReqDTO overdueProjectEventReqDTO = new OverdueProjectEventReqDTO();
		
		ProjectBorrowerSummary projectBorrowerSummary = projectBorrowerSummaryMapper.selectByPrimaryKey(refundNotifyDTO.getProjectId());
		
		// 基础数据
		overdueProjectEventReqDTO.setProjectId(projectBorrowerSummary.getProjectId());
		overdueProjectEventReqDTO.setUserId(projectBorrowerSummary.getBorrowerUserId());
		overdueProjectEventReqDTO.setTotalPeriods(projectBorrowerSummary.getTotalPeriod());
		overdueProjectEventReqDTO.setCurrentPeriods(projectBorrowerSummary.getCurrentPeriod());
		overdueProjectEventReqDTO.setOverdueStartDate(projectBorrowerSummary.getRefundDate());
		overdueProjectEventReqDTO.setUpdateDate(projectBorrowerSummary.getUpdateTime());
		overdueProjectEventReqDTO.setProjectStatusEventEnum(ProjectStatusEventEnum.enumOf(project.getStatus()));
		overdueProjectEventReqDTO.setProjectSubStatusEventEnum(ProjectSubStatusEventEnum.enumOf(project.getSubStatus()));
		
		// 标的历史最高逾期天数
		Integer overdueDay = projectBorrowerPlanNewMapper.selectMaxOverdueDayForHistory(projectBorrowerSummary.getProjectId(), projectBorrowerSummary.getBorrowerUserId());
		overdueProjectEventReqDTO.setOverdueDay(overdueDay);
		
		// 逾期金额计算
		RefundStateDTO refundStateDTO = feeCalcForRefundService.caculateAmountRepaymentForAPP(project.getProjectId(), com.niiwoo.civet.base.enums.RefundTypeEnum.IMMEDIATELY);
		overdueProjectEventReqDTO.setOverduePrincipal(refundStateDTO.getCapital());
		overdueProjectEventReqDTO.setOverdueInterest(refundStateDTO.getInterest());
		overdueProjectEventReqDTO.setGuaranteFee(refundStateDTO.getGuaranteeFee());
		overdueProjectEventReqDTO.setServiceFee(refundStateDTO.getManageFee());
		overdueProjectEventReqDTO.setOverdueFine(refundStateDTO.getPenaltyFee());
		
		return overdueProjectEventReqDTO;
	}
	
	@Override
	public RefundNotifyEnum getType() {
		return RefundNotifyEnum.ACTIVITY_EVENT_PROJECT_OVERDUE;
	}
}